<template>
  <el-form
    ref="formRef"
    v-loading="loading"
    element-loading-background="rgba(0,0,0,0.8)"
    :model="_form"
    :rules="rules"
    label-width="80px"
    size="small"
  >
    <el-form-item label="父节点" prop="parentId">
      <el-select v-model="_form.parentId" placeholder="请选择" filterable clearable>
        <el-option
          v-for="item in upperOptions"
          :key="item.mid"
          :label="item.title"
          :disabled="!isAdd && form.mid == item.mid"
          :level="item.level"
          :value="item.mid"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="类型">
      <el-radio-group v-model="_form.type">
        <el-radio label="M">目录</el-radio>
        <el-radio label="C">菜单</el-radio>
        <el-radio label="F">按钮</el-radio>
        <el-radio label="I">接口</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- <el-form-item prop="upper" style="display: none;">
          <el-input v-model="_form.upper" />
    </el-form-item>-->

    <el-form-item label="菜单名称" prop="title">
      <el-input v-model.trim="_form.title" />
    </el-form-item>
    <el-form-item label="页面地址" prop="path">
      <el-input v-model="_form.path" />
    </el-form-item>
    <el-form-item label="组件" prop="component">
      <el-input v-model="_form.component" />
    </el-form-item>
    <el-form-item label="重定向" prop="redirect">
      <el-input v-model="_form.redirect" />
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="_form.icon" />
    </el-form-item>
    <el-form-item label="显示" prop="visible">
      <el-checkbox v-model="_form.visible" />
    </el-form-item>
    <el-form-item label="不缓存" prop="noCache">
      <el-checkbox v-model="_form.noCache" />
    </el-form-item>

    <remark v-model:remark="_form.remark" />
    <el-form-item>
      <drawer-footer :save-loading="saveLoading" @cancel="cancel" @confirm="confirm" />
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { computed, defineEmits, defineExpose, defineProps, PropType, ref, watch } from 'vue'

import { ElMessage } from 'element-plus'

import DrawerFooter from '&/components/Drawer/Footer.vue'
import Remark from '&/components/Form/remark.vue'

import MenuApi from '@/api/backend/menu'

import { commonTitleReg } from '@/utils/regExp'

import IBaseObj from '#/index'

const rules = {
  title: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { max: 20, message: '请输入长度不超过20个字符', trigger: 'blur' },
    { ...commonTitleReg, trigger: 'blur' },
  ],
  parentId: [{ required: true, message: '请选择父节点', trigger: 'change' }],
  path: [{ required: true, message: '请输入地址', trigger: 'blur' }],
  // icon: [{ max: 20, message: '请输入长度超过20个字符', trigger: 'blur' }],
  component: [{ required: true, message: '请输入组件', trigger: 'blur' }],
}
const props = defineProps({
  form: {
    required: true,
    type: Object,
  },
  upperOptions: {
    type: Array as PropType<IBaseObj[]>,
    default: () => [],
  },
  isAdd: {
    type: Boolean,
    default: true,
  },
  loading: {
    type: Boolean,
    default: false,
  },
})

const emits = defineEmits(['update:form', 'cancel', 'confirm'])

const _form = computed({
  get() {
    return props.form
  },
  set() {
    emits('update:form')
  },
})

const saveLoading = ref(false)

const cancel = () => {
  emits('cancel')
}
const formRef = ref()
const confirm = () => {
  formRef.value.validate(async (valid: Boolean) => {
    if (valid) {
      saveLoading.value = true
      try {
        const res = await MenuApi.saveMenu({ ..._form.value })

        const { status } = res
        if (status === 200) {
          if (props.isAdd) {
            ElMessage({ message: '创建成功!', type: 'success', duration: 1500 })
          } else {
            ElMessage({ message: '修改成功!', type: 'success', duration: 1500 })
          }
          emits('confirm')
        }
      } catch (error) {
        console.log(error)
      } finally {
        saveLoading.value = false
      }
    } else {
      return false
    }
  })
}

const clear = () => {
  formRef.value.resetFields()
  console.log(2)
}

defineExpose({
  clear,
})
</script>
